//
//  ViewController.m
//  05-UIScrollView
//
//  Created by LL.F on 16/7/20.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    /*
     *
     *   UIScrollView 滚动视图 继承于UIView
     *
     */
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenbounds];
    scrollView.backgroundColor = [UIColor yellowColor];
    // 1.可以滚动的范围内大小(范围足够大才有滚动条) 默认是CGSizeZero = CGSizeMake(0, 0);
    scrollView.contentSize = CGSizeMake(0, 1000);// 宽设为0后,只能纵向滚
    // 2.偏移量 滚动的位置 默认是CGSizeZero = CGPointMake(0, 0);
    // 不设置contentSize contentOffset也可以改变, 改变contentOffset改变的是????? 实质上是对scrollView的bounds进行修改
    NSLog(@"offset改变前 scrollView frame is : %@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset改变前 scrollView bounds is : %@", NSStringFromCGRect(scrollView.bounds));
    scrollView.contentOffset = CGPointMake(0, 500);
    NSLog(@"offset改变后 scrollView frame is : %@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset改变后 scrollView bounds is : %@", NSStringFromCGRect(scrollView.bounds));
    // 3.添加滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //4. 锁定垂直或水平(45°角, 锁会失效)
    //scrollView.directionalLockEnabled = YES;//锁定垂直或者水平滚动
    // 5.回弹效果
    scrollView.bounces = YES;
    // 6.alwaysBounceVertical
    // 在没有垂直方向上的滚动范围时 保证垂直方向的回弹效果存在
    scrollView.alwaysBounceVertical = YES;
    // 7.alwaysBounceHorizontal
    // 在没有水平方向上的滚动范围时 保证水平方向的回弹效果存在
    scrollView.alwaysBounceHorizontal = YES;
    // 8.设置能否滚动 默认YES
    scrollView.scrollEnabled = YES;
    // 9.按页滚动 默认YES
    scrollView.pagingEnabled = YES;
    // 10.显示垂直或水平滚动指示器 默认YES
    scrollView.showsVerticalScrollIndicator = YES;
    scrollView.showsHorizontalScrollIndicator = YES;
    // 11.滚动指示器的边距
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
    // 12.滚动指示器的风格(枚举:0 default 适用于各种颜色背景 1 black适用于浅色背景 2 white适用于深色背景)-->可以应用于夜间模式
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    // 13. 默认都为yes
    // tracking 用户是否开始拖动
//    scrollView.tracking
    // dragging 用户是否在拖动中
//    scrollView.dragging
    // decelerating 是否减速中
//    scrollView.decelerating
    // 14.双击状态栏回到滚动视图顶端的手势是否存在 默认为YES
    scrollView.scrollsToTop = YES;

    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 10, kScreenWidth - 40, 100);
    button.backgroundColor = [UIColor redColor];
    [scrollView addSubview:button];
    [self.view addSubview:scrollView];
    
    // 设置代理人
    scrollView.delegate = self;
//    键盘小时模式
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
//    练习
    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(10, 300, kScreenWidth - 20, 200)];
    field.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:field];
    
//    如果想进行试图缩放，必须实现viewForZoomingInScrollView
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2;
}
//与其想要的缩放的姿势图绑定ScrollView的contentSize会跟随这个子视图变化
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView.subviews firstObject];
}

#pragma mark --滚动视图滚动就会执行此方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 用isDragging属性可以判断视图的滚动是用户拖拽产生的还是自己滚动的
    if (scrollView.isDragging) {
        NSLog(@"正在拖动中");
    } else
    NSLog(@"自己滚动中");
//    用户触摸结束decelerating
    if (scrollView.decelerating) {
//        NSLog(@"%");
    }
    
}
#pragma mark --将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"开始拖拽");
}
#pragma mark --已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"结束拖拽");
}
#pragma mark --将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"开始减速");
}
#pragma mark --已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"结束减速");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
